package com.onlinemovies.controller;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.onlinemovies.entity.BookingDetails;
import com.onlinemovies.entity.HallDetails;
import com.onlinemovies.entity.MovieDetails;
import com.onlinemovies.entity.ShowDetails;
import com.onlinemovies.entity.User;
import com.onlinemovies.service.HallService;
import com.onlinemovies.service.MovieService;

@Controller
@RequestMapping(value={"/movie"})
public class MovieController  {
	
	@Autowired	
	private MovieService movieService;
	
	@Autowired
	private HallService hallService;
	
	@RequestMapping(value = { "", "/" })
	public ModelAndView moviedetails() throws Exception {

		Map<String, Object> params = new HashMap<String, Object>();

		List<MovieDetails> movies = movieService.getAll();

		params.put("movies", movies);

		params.put("movie", new MovieDetails());
		
		List<HallDetails> all = hallService.getAll();
		params.put("hallList", all);

		return new ModelAndView("movie", params);
	}

	@RequestMapping("/insert")
	public ModelAndView insert(@ModelAttribute("movie") MovieDetails movie)
			throws Exception {

		Map<String, Object> params = new HashMap<String, Object>();

		if (!StringUtils.hasText(movie.getMoviename())) {

			params.put("movienameMessage", "Input required");

			//return new ModelAndView("hall", params);
			return new ModelAndView("redirect:/movie");


		}

		movieService.doInsert(movie);

		//return new ModelAndView("hall",null);
		return new ModelAndView("redirect:/movie");
	}


	@RequestMapping("/about")
	public ModelAndView about() throws Exception {
		return new ModelAndView("about", null);
	}
	
	@RequestMapping("/savetocart")
	public ModelAndView savetocart(@ModelAttribute("bookingdetails") BookingDetails bookingDetails,HttpSession httpSession)
			throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		try {
			User value=(User) httpSession.getAttribute("user");
			
			if(value==null){
				params.put("bookingDetailsMessage", "Please login to book your ticket.");
				bookingDetails.setUserDetails(value);
			}else{
			String message=movieService.doInsertBookingDetails(bookingDetails);
			params.put("bookingDetailsMessage", message);
			SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy");
			String date = sdf.format(new Date());
			params.put("movienshowdate", date);

			List<BookingDetails> bookingDetails1 = movieService.getBookingDetails(value);

			params.put("bookingDetails", bookingDetails1);
			}
				return new ModelAndView("movieCityLanguage", params);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			params.put("bookingDetailsMessage", "Booking failed due to some reason");
		}
		
		return new ModelAndView("movieCityLanguage", null);

		}
	
	@RequestMapping(value = "/getmovieslist", method = RequestMethod.GET, headers="Accept=application/json")
	public @ResponseBody
	Set<MovieDetails> citiesForState(
			@RequestParam(value = "hallid", required = true) Integer hallid) {
		Set<MovieDetails> findMovieForHall = movieService.findMovieForHall(hallid);
		return findMovieForHall;
	}
	
	@RequestMapping(value = "/getshowlist", method = RequestMethod.GET, headers="Accept=application/json")
	public @ResponseBody
	Set<ShowDetails> showsForState(
			@RequestParam(value = "movieid", required = true) Integer movieid) {
		Set<ShowDetails> findShowForMovie = movieService.findfindShowForMovie(movieid);
		return findShowForMovie;
	}
	
	@RequestMapping(value = "/calculateprice", method = RequestMethod.GET, headers="Accept=application/json")
	public @ResponseBody
	String calculateprice(
			@RequestParam(value = "showid", required = true) Integer showid) {
		String price = movieService.findPriceForMovie(showid);
		return price;
	}
}
